/*
 * 项目名称:platform-plus
 * 类名称:StaffController.java
 * 包名称:com.platform.modules.staff.controller
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2020-10-10 14:34:32        klwu     初版做成
 *
 * Copyright (c) 2019-2019 klwu
 */
package com.platform.modules.custmoer.controler;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.utils.DateUtils;
import com.platform.common.utils.RestResponse;
import com.platform.modules.custmoer.dto.MyCustomersDto;
import com.platform.modules.custmoer.dto.StaffOption;
import com.platform.modules.custmoer.entity.CustomerEntity;
import com.platform.modules.custmoer.entity.StaffEntity;
import com.platform.modules.custmoer.entity.StaffMapEntity;
import com.platform.modules.custmoer.service.CustomerService;
import com.platform.modules.custmoer.service.StaffMapService;
import com.platform.modules.custmoer.service.StaffService;
import com.platform.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Controller
 *
 * @author klwu
 * @date 2020-10-10 14:34:32
 */
@RestController
@RequestMapping("staff/")
public class StaffController extends AbstractController {
    @Autowired
    private StaffService staffService;
    @Autowired
    private StaffMapService staffMapService;
    @Autowired
    private CustomerService customerService;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("staff::list")
    public RestResponse queryAll(@RequestParam Map<String, Object> params) {
        List<StaffEntity> list = staffService.queryAll(params);

        return RestResponse.success().put("list", list);
    }

    /**
     * 全国员工和今日新增
     * @param params
     * @return
     */
    @RequestMapping("/queryAllCount")
    public RestResponse All(@RequestParam Map<String, Object> params) {
        List<StaffEntity> list = staffService.queryAll(params);
        List<StaffEntity> todayCoust = list.stream().filter(s -> DateUtils.format(s.getCreateTime(),DateUtils.DATE_PATTERN).equals(DateUtils.format(new Date(),DateUtils.DATE_PATTERN))).collect(Collectors.toList());
        return RestResponse.success().put("total", list.size()).put("today",todayCoust.size());
    }


    /**
     * 分页查询
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @GetMapping("/list")
    @RequiresPermissions("staff::list")
    public RestResponse list(@RequestParam Map<String, Object> params) {
        Page page = staffService.queryPage(params);

        return RestResponse.success().put("page", page);
    }

    /**
     * 根据主键查询详情
     *
     * @param id 主键
     * @return RestResponse
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("staff::info")
    public RestResponse info(@PathVariable("id") String id) {
        StaffEntity staff = staffService.getById(id);

        List<StaffMapEntity> mapRecordList = new ArrayList<>();
        if(staff != null){
            Map<String, Object> params = new HashMap<>();
            params.put("staffId",staff.getId());
            mapRecordList = staffMapService.queryAll(params);
            List<CustomerEntity> customerEntityList = customerService.queryAll(params);
            // 昨日总数
            List<CustomerEntity> preCust = customerEntityList.stream()
                    .filter(s -> !DateUtils.format(s.getCreateTime(),DateUtils.DATE_PATTERN)
                            .equals(DateUtils.format(new Date(),DateUtils.DATE_PATTERN)))
                    .collect(Collectors.toList());
        }
        return RestResponse.success().put("data", staff).put("map",mapRecordList);
    }

    /**
     * 新增
     *
     * @param staff staff
     * @return RestResponse
     */
    @SysLog("新增")
    @RequestMapping("/save")
    @RequiresPermissions("staff::save")
    public RestResponse save(@RequestBody StaffEntity staff) {

        staff.setCreateTime(new Date());
        staffService.add(staff);

        return RestResponse.success();
    }

    /**
     * 修改
     *
     * @param staff staff
     * @return RestResponse
     */
    @SysLog("修改")
    @RequestMapping("/update")
    public RestResponse update(@RequestBody StaffEntity staff) {

        staffService.update(staff);

        return RestResponse.success();
    }

    /**
     * 根据主键删除
     *
     * @param ids ids
     * @return RestResponse
     */
    @SysLog("删除")
    @RequestMapping("/delete")
    //@RequiresPermissions("staff::delete")
    public RestResponse delete(@RequestBody String[] ids) {
        staffService.deleteBatch(ids);

        return RestResponse.success();
    }


    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @RequestMapping("/findAll")
    public RestResponse findall(@RequestParam Map<String, Object> params) {
        List<StaffEntity> list = staffService.queryAll(params);
        List<StaffOption> collect = list.stream().map(e -> {
            StaffOption staffOption = new StaffOption();
            staffOption.setLabel(e.getName());
            staffOption.setValue(e.getId());
            return staffOption;
        }).collect(Collectors.toList());
        return RestResponse.success().put("list", collect);
    }

    @GetMapping("/home/staffs")
    public RestResponse queryByDays(@RequestParam Map<String, Object> params){
        params.put("diff",7);
        List<String> days = customerService.getLastDays(params);
        List<Integer> nums = new ArrayList();
        List<StaffEntity> staffEntities = staffService.queryByDays(params);
        int one = 0;
        int two = 0;
        int thrid = 0;
        int four = 0;
        int five = 0;
        int six = 0;
        int sev = 0;
        for(StaffEntity my : staffEntities){
            String format = DateUtils.format(my.getCreateTime(), DateUtils.DATE_PATTERN);
            if(format.equals(days.get(0))){
                one ++;
            }
            if(format.equals(days.get(1))){
                two ++;
            }
            if(format.equals(days.get(2))){
                thrid ++;
            }
            if(format.equals(days.get(3))){
                four ++;
            }
            if(format.equals(days.get(4))){
                five ++;
            }
            if(format.equals(days.get(5))){
                six ++;
            }
            if(format.equals(days.get(6))){
                sev ++;
            }
        }
        nums.add(one);
        nums.add(two);
        nums.add(thrid);
        nums.add(four);
        nums.add(five);
        nums.add(six);
        nums.add(sev);
        return RestResponse.success().put("data",staffEntities).put("staffnums",nums);
    }

}
